package com.bawei.composeui2207a.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bawei.composeui2207a.day02.Page9

/**
 * 作者：赵亮
 * 时间：2024/12/11 16:49
 * 功能：门户页面
 */
data class UserInfo(val name:String, var age:Int)
@Composable
fun HomePage() {

    var list = remember {
        SnapshotStateList<UserInfo>()
    }

    var refresh by remember {
        mutableStateOf(Math.random())
    }

    repeat(10) {
        list.add(UserInfo("孙悟空", it))
    }

    Column {
        Row {
            Box(
                modifier = Modifier
                    .size(100.dp)
                    .clip(CircleShape)
                    .background(color = Color.Gray)
                    .clickable {
                         list.add(0, UserInfo("猪八戒", 1))
                    }
            ) {
                Text(text = "+", fontSize = 30.sp, modifier = Modifier.align(Alignment.Center))
            }
            LazyRow {
                items(list) {
                    Box(
                        modifier = Modifier
                            .size(100.dp)
                            .clip(CircleShape)
                            .background(color = Color.Gray)
                            .clickable {
                                it.age = it.age + 1
                                refresh = Math.random()
                            }
                    ) {
                        Text(text = "$refresh", modifier = Modifier.size(0.dp))
                        Text(text = "${it.name}${it.age}", modifier = Modifier.align(Alignment.Center))
                    }
                }
            }
        }
    }
}

@Preview
@Composable
fun HomePagePreview() {
    HomePage()
}

